import { ref, computed } from 'vue'
import useUserStore from '@/store/modules/user'
import { 
  isSuperAdmin, 
  isQualityManager, 
  isInspector, 
  hasFeatureAccess, 
  getVisibleActions,
  hasPermission,
  hasRole 
} from '@/utils/auth'

/**
 * 权限管理组合式函数
 * @returns {object} 权限相关的响应式数据和方法
 */
export function usePermission() {
  const userStore = useUserStore()

  // 用户角色判断
  const isAdmin = computed(() => isSuperAdmin())
  const isQualityMgr = computed(() => isQualityManager())
  const isInsp = computed(() => isInspector())

  // 获取当前用户信息
  const currentUser = computed(() => userStore.userInfo)
  const currentRoles = computed(() => userStore.roles || [])
  const currentPermissions = computed(() => userStore.permissions || [])

  // 权限检查方法
  const checkFeatureAccess = (feature) => hasFeatureAccess(feature)
  const checkPermission = (permissions) => hasPermission(permissions)
  const checkRole = (roles) => hasRole(roles)

  // 获取模块可见的操作按钮
  const getModuleActions = (module) => getVisibleActions(module)

  // 检查按钮是否可见
  const isActionVisible = (module, action) => {
    const visibleActions = getVisibleActions(module)
    return visibleActions.includes(action)
  }

  // 基于角色的功能访问权限
  const permissions = computed(() => ({
    // 基础数据管理
    canManageProduct: checkFeatureAccess('product:manage'),
    canViewProduct: checkFeatureAccess('product:view'),
    canManageProcess: checkFeatureAccess('process:manage'),
    canViewProcess: checkFeatureAccess('process:view'),
    canManageStandard: checkFeatureAccess('standard:manage'),
    canViewStandard: checkFeatureAccess('standard:view'),
    canManageEquipment: checkFeatureAccess('equipment:manage'),
    canViewEquipment: checkFeatureAccess('equipment:view'),

    // 质量检验
    canCreateInspectionTask: checkFeatureAccess('inspection:task:create'),
    canAssignInspectionTask: checkFeatureAccess('inspection:task:assign'),
    canViewInspectionTask: checkFeatureAccess('inspection:task:view'),
    canEntryInspectionData: checkFeatureAccess('inspection:data:entry'),
    canViewInspectionData: checkFeatureAccess('inspection:data:view'),
    canManageDefect: checkFeatureAccess('inspection:defect:manage'),
    canViewDefect: checkFeatureAccess('inspection:defect:view'),

    // 不合格品管理
    canManageNonconforming: checkFeatureAccess('nonconforming:manage'),
    canRegisterNonconforming: checkFeatureAccess('nonconforming:register'),
    canViewNonconforming: checkFeatureAccess('nonconforming:view'),

    // 质量证书
    canManageCertificate: checkFeatureAccess('certificate:manage'),
    canViewCertificate: checkFeatureAccess('certificate:view'),

    // 数据分析
    canFullAnalysis: checkFeatureAccess('analysis:full'),
    canBasicAnalysis: checkFeatureAccess('analysis:basic'),

    // 系统管理
    canManageUser: checkFeatureAccess('system:user:manage'),
    canManageRole: checkFeatureAccess('system:role:manage'),
    canManageMenu: checkFeatureAccess('system:menu:manage'),
    canManageConfig: checkFeatureAccess('system:config:manage'),
    canViewSystem: checkFeatureAccess('system:view')
  }))

  return {
    // 响应式数据
    isAdmin,
    isQualityMgr,
    isInsp,
    currentUser,
    currentRoles,
    currentPermissions,
    permissions,

    // 方法
    checkFeatureAccess,
    checkPermission,
    checkRole,
    getModuleActions,
    isActionVisible
  }
}

/**
 * 页面权限检查组合式函数
 * @param {string} moduleName 模块名称
 * @returns {object} 页面权限相关的响应式数据
 */
export function usePagePermission(moduleName) {
  const { isActionVisible, permissions } = usePermission()

  // 页面操作按钮权限
  const pageActions = computed(() => ({
    canAdd: isActionVisible(moduleName, 'add'),
    canEdit: isActionVisible(moduleName, 'edit'),
    canDelete: isActionVisible(moduleName, 'delete'),
    canView: isActionVisible(moduleName, 'view'),
    canExport: isActionVisible(moduleName, 'export'),
    canImport: isActionVisible(moduleName, 'import'),
    canAssign: isActionVisible(moduleName, 'assign'),
    canGenerate: isActionVisible(moduleName, 'generate')
  }))

  return {
    pageActions,
    permissions
  }
}

/**
 * 表格操作列权限组合式函数
 * @param {string} moduleName 模块名称
 * @returns {object} 表格操作相关的权限数据
 */
export function useTableActions(moduleName) {
  const { isActionVisible } = usePermission()

  // 表格行操作按钮配置
  const tableActions = computed(() => {
    const actions = []
    
    if (isActionVisible(moduleName, 'edit')) {
      actions.push({
        label: '修改',
        type: 'primary',
        icon: 'Edit',
        action: 'edit',
        permission: `${moduleName}:edit`
      })
    }
    
    if (isActionVisible(moduleName, 'delete')) {
      actions.push({
        label: '删除',
        type: 'danger',
        icon: 'Delete',
        action: 'delete',
        permission: `${moduleName}:remove`
      })
    }
    
    if (isActionVisible(moduleName, 'view')) {
      actions.push({
        label: '详情',
        type: 'primary',
        icon: 'View',
        action: 'view',
        permission: `${moduleName}:query`
      })
    }

    return actions
  })

  return {
    tableActions
  }
}

export default {
  usePermission,
  usePagePermission,
  useTableActions
} 